
package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjtemp;
import com.chezhujie.www.service.CzjtempService;
import com.xy.core.page.Pagination;
import com.xy.core.utils.UUIDUtils;
import com.xy.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/czjtemp")
public class CzjtempController extends BaseController<Czjtemp> {

	@Autowired
	private CzjtempService czjtempService;
	
    /**
     * 显示所有
     * @param req
     * @param resp
     * @param model
     * @return
     */
	 @RequestMapping("/list")
	    public String list(
	            HttpServletRequest req, HttpServletResponse resp,
	                       ModelMap model){
	        try {
				model.addAttribute("msg", req.getParameter("msg"));
				Pagination pagination = czjtempService.list();
				model.addAttribute("pagination", pagination);
			} catch (Exception e) {
				e.printStackTrace();
			}
	        return "czjtemp/list";
	    }
	 /**
	  * 交换位置
	  * @param sorting
	  * @param id
	  * @param req
	  * @param resp
	  * @param model
	  * @return
	  * @throws IOException
	  */
	 @RequestMapping("/exchange")
	    public String exchange(@RequestParam(required = true) Integer sorting,
	                         @RequestParam(required = true) String id,
	            HttpServletRequest req, HttpServletResponse resp,
	                       ModelMap model) throws IOException {
	        Map<String, Object> map = new HashMap<String, Object>();
	        String msg = "";
	        try {
	            if(sorting==1||sorting==-1){
	            	czjtempService.exchange1(id,sorting);
	                map.put("status", "1");
	            }else{
	                throw new Exception("参数sorting只能传-1或1");
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            map.put("status", "0");
	            msg =""+ e.getMessage();
	        }
	        return "redirect:list.do?msg="+msg;

	    }
	 
	 /**
	  * 假删除
	  * @param templateIdList
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping("delete")
		public String delete(@RequestParam (required = true) String dataresourceId,
				@RequestParam (required = true) Integer op,
				HttpServletRequest request, HttpServletResponse response) {

			try {
				czjtempService.deleteById(dataresourceId,op);
			} catch (Exception e) {
				log.error("error", e);
				e.printStackTrace();
			}
			return "redirect:/czjtemp/list";
		}
	
	 /**
	  * 详情
	  * @param templateId
	  * @param request
	  * @param response
	  * @param model
	  * @return
	  */
	 @RequestMapping("detail")
		public String detail(java.lang.String dataresourceId,
				HttpServletRequest request, HttpServletResponse response,
				ModelMap model) {

			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("dataresourceId", dataresourceId);

				Czjtemp czjtemp = czjtempService.getTempById(map);
				model.addAttribute("czjtemp", czjtemp);
			} catch (Exception e) {
				log.error("error", e);
				e.printStackTrace();

			}
			return "czjtemp/detail";
		}
	 
	 /**
	  * 修改页面
	  * @param templateId
	  * @param pageNo
	  * @param pageSize
	  * @param order
	  * @param sort
	  * @param request
	  * @param response
	  * @param model
	  * @return
	  */
		@RequestMapping("goupdate")
		public String goupdate(String dataresourceId, Integer pageNo, Integer pageSize,
				String order, String sort, HttpServletRequest request,
				HttpServletResponse response, ModelMap model) {

			try {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("dataresourceId", dataresourceId.trim());
				Czjtemp czjtemp = czjtempService.getTempById(map);
				model.addAttribute("czjtemp", czjtemp);

			} catch (Exception e) {
				e.printStackTrace();
				log.error("error", e);
			}

			return "czjtemp/update";

		}
		
		
		/**
		 * 修改
		 * @param czjtemplate
		 * @param pageNo
		 * @param pageSize
		 * @param order
		 * @param file
		 * @param sort
		 * @param request
		 * @param response
		 * @param model
		 * @return
		 */
		@RequestMapping("doupdate")
		public String doupdate(
				Czjtemp czjtemp,
				Integer pageNo,
				Integer pageSize,
				String order,
				@RequestParam(value = "file", required = false) MultipartFile[] file,
				String sort, HttpServletRequest request,
				HttpServletResponse response, ModelMap model) {

			try {
				
				String pathRoot = request.getSession().getServletContext()
						.getRealPath("");
				
				File fileDir = new File(pathRoot + "/upload");
				if (!fileDir.exists()) {
					fileDir.mkdir();
				}
			

				for (int i = 0; i < file.length; i++) {
					String path = "/upload/" + UUIDUtils.getUUID();// 数据库保存路径

					if(i==0 && file[i].getSize()>0){
						file[i].transferTo(new File(pathRoot + path+".jpg"));
						
						/*ImageUtils.fromFile(new File(pathRoot + path+".jpg"))
						.quality(0.6f)
						.fixedGivenSize(true)
						.keepRatio(true)
						.bgcolor(Color.WHITE)	//透明背景
						.toFile(new File(pathRoot + path+"czj.jpg"));
						
						czjtemp.setPicPath(path+"czj.jpg");*/
						czjtemp.setPicPath(path+".jpg");
					}
					if(i==1 && file[i].getSize()>1){
						
						file[i].transferTo(new File(pathRoot + path+".jpg"));
						
					/*	ImageUtils.fromFile(new File(pathRoot + path+".jpg"))
						.quality(0.6f)
						.fixedGivenSize(true)
						.keepRatio(true)
						.bgcolor(Color.WHITE)	//透明背景
						.toFile(new File(pathRoot + path+"czj.jpg"));
						
						czjtemp.setBackgroundPath(path+"czj.jpg");*/
						czjtemp.setBackgroundPath(path+".jpg");
					}
					if(i==2 && file[i].getSize()>2){
						
						
						file[i].transferTo(new File(pathRoot + path+".jpg"));
						
					/*	ImageUtils.fromFile(new File(pathRoot + path+".jpg"))
						.quality(0.6f)
						.fixedGivenSize(true)
						.keepRatio(true)
						.bgcolor(Color.WHITE)	//白色背景
						.toFile(new File(pathRoot + path+"czj.jpg"));
						
						czjtemp.setSweepImgPath(path+"czj.jpg");*/
						czjtemp.setSweepImgPath(path+".jpg");
					}
					

				}
				if (null == czjtemp.getDataresourceId()
						|| "".equals(czjtemp.getDataresourceId())) { // 新增

				} else { // 修改
					if(StringUtils.isNotBlank(czjtemp.getVideoUrl())){
						String url=czjtemp.getVideoUrl();
						if(!url.startsWith("http://") && !url.equals("https://")){
							url="http://"+url;
							czjtemp.setVideoUrl(url);
						}
					}
					czjtempService.updateTemp(czjtemp);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("error", e);
			}
				return "redirect:/czjtemp/list";

		}
			
			
		/*	try {
				String pathRoot = request.getSession().getServletContext()
						.getRealPath("");

				for (int i = 0; i < file.length; i++) {
					String path = "/upload/" + UUIDUtils.getUUID() + ".jpg";// 数据库保存路径

					if(i==0 && file[i].getSize()>0){
						
						czjtemp.setPicPath(path);
					}
					if(i==1 && file[i].getSize()>1){
						
						czjtemp.setBackgroundPath(path);
					}
					if(i==2 && file[i].getSize()>2){
						
						czjtemp.setSweepImgPath(path);
					}
					
					File fileDir = new File(pathRoot + "/upload");
					if (!fileDir.exists()) {
						fileDir.mkdir();
					}
					file[i].transferTo(new File(pathRoot + path));

				}
				if (null == czjtemp.getDataresourceId()
						|| "".equals(czjtemp.getDataresourceId())) { // 新增

				} else { // 修改
					if(StringUtils.isNotBlank(czjtemp.getVideoUrl())){
						String url=czjtemp.getVideoUrl();
						if(!url.startsWith("http://") && !url.equals("https://")){
							url="http://"+url;
							czjtemp.setVideoUrl(url);
						}
					}
					czjtempService.updateTemp(czjtemp);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("error", e);
			}


			return "redirect:/czjtemp/list";*/
	

		
}
